#include <iostream>
//#include "situation.h"
#include "../situationControl/Situation.h"
#include "../situationControl/Player.h"
#include "../situationControl/Room.h"
#include "networkHandler.h"
#include <boost/thread/thread.hpp>
#include "../situationControl/SituationController.h"
//using networkI;

bool compareSituations(Situation sentSituation, Situation receivedSituation) {
	bool success = true;
	std::cout << "Originally Sent Situation Player X Pos: " << sentSituation.getPlayer().getXPos() << std::endl;
	std::cout << "Received Situation Player X Pos: " << receivedSituation.getPlayer().getXPos() << std::endl;

	if(sentSituation.getPlayer().getXPos() != receivedSituation.getPlayer().getXPos())
		success = false;
	if(sentSituation.getPlayer().getYPos() != receivedSituation.getPlayer().getYPos())
		success = false;
	if(sentSituation.getPlayer().getZPos() != receivedSituation.getPlayer().getZPos())
		success = false;

	if(sentSituation.getOpponent().getXPos() != receivedSituation.getOpponent().getXPos())
		success = false;
	if(sentSituation.getOpponent().getYPos() != receivedSituation.getOpponent().getYPos())
		success = false;
	if(sentSituation.getOpponent().getZPos() != receivedSituation.getOpponent().getZPos())
		success = false;

	if(sentSituation.getRoom().getHeight() != receivedSituation.getRoom().getHeight())
		success = false;
	if(sentSituation.getRoom().getWidth() != receivedSituation.getRoom().getWidth())
		success = false;
	if(sentSituation.getRoom().getDepth() != receivedSituation.getRoom().getDepth())
		success = false;

	return success;
}

Situation addTestData(){
	// Create a player, opponent, and room
	std::cout << "Creating player, enemy, room\n";
	Player myPlayer;
	myPlayer.setXPos(33.15);
	// Print out the player's x position just as an example that the data is correct
	std::cout << "Setting player's x position to "<< myPlayer.getXPos() << std::endl;
	myPlayer.setYPos(89.76);
	myPlayer.setZPos(54.321);

	Player myOpponent;
	myOpponent.setXPos(33.15);
	myOpponent.setYPos(89.76);
	myOpponent.setZPos(54.321);

	Room myRoom(28.33, 19.76, 11.5);

	Situation testSituation;

	testSituation.setPlayer(myPlayer);
	testSituation.setOpponent(myOpponent);
	testSituation.setRoom(myRoom);

	return testSituation;
}

int main()
{
	// Create a situation containing the player, opponent, and room
	Situation mySituation=addTestData();

	// Create Network Handler and send a situation
	//SituationController *mSC = new SituationController();
        //networkI::networkHandler myH(mSC);
	networkI::networkHandler *myH = new networkI::networkHandler();
	//networkI::networkHandler *myH = new networkI::networkHandler(mSC);
	std::cout << "Connecting to server\n";
	boost::thread t(boost::bind(&boost::asio::io_service::run, myH->getIOservice()));
	
        myH->sendSituation(mySituation);

	// Sleep for 10,000 microseconds (arbitrary number) to all the situation to be received before closing connection
	// Anoth method is to not call the close here, and then ctrl+C the server
	usleep(10000);

	//recieve situation and close connection
	Situation receivedSituation = myH->MySituation;

	myH->close();
	t.join();

	// Ensure that the situation received is the same one that was sent
	std::cout << "__________COMPARING SITUATIONS_____________\n";
	bool success = compareSituations(mySituation, receivedSituation);
	if (success)
		std::cout << "Test passed\n";
	else
		std::cout << "Test failed\n";

        return 0;
}
